﻿function nuevaFila(tipoCosto) {
    var tabla;
    if (tipoCosto == 1) tabla = 'Travelling';
    if (tipoCosto == 2) tabla = 'Working';
    if (tipoCosto == 3) tabla = 'Waiting';
    if ($('#tablaCostos' + tabla + ' tbody > tr').length == 1) {
        var fila = '<tr>' +
                '<td><input class="form-control customDate reset" maxlength="35" name="fecha" type="text" value=""></td>' +
                '<td><input class="form-control reset customTime horaIni" name="horaDesde" type="text" value=""></td>' +
                '<td><input class="form-control reset customTime horaFin" name="horaHasta" type="text" value=""></td>' +
                '<td><input class="form-control reset nHoras" name="nHoras" readonly="readonly" type="text" value=""></td>' +
                '<td><textarea class="form-control reset" name="descripcion"></textarea></td>' +
                '<td><input class="form-control reset" name="surveyor" type="text" value=""></td>' +
                '<td><input class="form-control reset subtotal" name="subtotal" type="text" readonly="readonly" value=""></td>' +
                '<td>' +
                '    <a style="vertical-align: top" onclick="javascript:borrarFila(this,1)" >' +
                '    <span style="vertical-align: central;line-height: 100%;" class="glyphicon glyphicon-remove"></span>' +
                '    </a>' +
                '    <input name="tipoCosto" type="hidden" value="'+tipoCosto+'">' +
                '</td>' +
            '</tr>';
        $('#tablaCostos' + tabla + ' tbody > tr').after(fila);
        var aux = $('#tablaCostos' + tabla + ' tbody > tr');
        aux.find(".customDate").datepicker();
        aux.find(".customTime").timepicker({
            defaultTime: false,
            minuteStep: 5,
            showMeridian: false,
            disableFocus: true
        });
    } else {
        var clon = $('#tablaCostos' + tabla + ' tbody>tr:last').clone(false);
        clon.insertAfter('#tablaCostos' + tabla + ' tbody>tr:last');
        for (var i = 0; i < clon.find(".reset").length ; i++) {
            if (clon.find(".reset").length == 1)
                clon.find(".reset").value = '';
            else
                clon.find(".reset")[i].value = '';
        }
        clon.find(".reset").removeAttr("id");
        clon.find(".customDate").removeClass('hasDatepicker');
        clon.find(".customDate").datepicker();
        clon.find(".customTime").timepicker({
            defaultTime: false,
            minuteStep: 5,
            showMeridian: false
        });
        return false;
    }
}
function borrarFila(boton,tipoCosto) {
    var par = $(boton).parent().parent(); //tr
    par.remove();
}

function valorEnMin(tiempo) {
    var hora = tiempo.substring(0, 2)*1;
    var minutos = tiempo.substring(3, 5)*1;
    return hora * 60 + minutos;
}

function horaMayor(hora1, hora2) {
    return valorEnMin(hora1) >= valorEnMin(hora2);
}

function hallarCostoExtra(fecha) {
    var day = fecha.substring(0, 2);
    var month = fecha.substring(3, 5)-1;
    var year = fecha.substring(6,10)
    var d = new Date(year, month, day, 0, 0, 0, 0);
    var n = d.getDay();
    if (n == 0 || n == 6)
        return 1.3;
    return 1;
}

function actualizar() {
    var listaTravel = $('#tablaCostosTravelling .customDate');
    for (var i = 0; i < listaTravel.length; i++) {
        calcularSubtotal(listaTravel[i],1)
    }
    var listaWork = $('#tablaCostosWorking .customDate');
    for (var i = 0; i < listaWork.length; i++) {
        calcularSubtotal(listaWork[i], 2)
    }
    var listaWait = $('#tablaCostosWaiting .customDate');
    for (var i = 0; i < listaWait.length; i++) {
        calcularSubtotal(listaWait[i], 1)
    }
}

function checkflat(control) {
    var par = $(control).parent().parent();
    var subtotal = par.find(".subtotal")[0];
    subtotal.readOnly = !control.checked;
    

}

function calcularSubtotal(control, tipoCosto) {
    tipoCosto = tipoCosto == 3 ? 1 : tipoCosto;
    var par = $(control).parent().parent();
    var isflat = par.find(".isflat")[0].checked;
    if (isflat) return;
    var rate = $("#Rate").val();
    var horaIni = par.find(".horaIni").val();
    var horaFin = par.find(".horaFin").val();
    var nHoras = par.find(".nHoras")[0];
    var subtotal = par.find(".subtotal")[0];
    var dia = par.find(".customDate").val();

    if (horaFin == "" || horaIni == "" || rate == "" || dia == "") {
        subtotal.value = 0;
        nHoras.value = 0;
        return;
    }
    if (horaMayor(horaIni, horaFin)) {
        subtotal.value = 0;
        nHoras.value = 0;
        return;
    }
    nHoras.value = (valorEnMin(horaFin) - valorEnMin(horaIni)) * 1.0 / 60.0;
    var costoExtra = hallarCostoExtra(dia);
    var horasCobradas = (nHoras.value >= 12 ? 12 : nHoras.value);
    subtotal.value = horasCobradas * 1.0 * rate * 1.0 * tipoCosto / 2.0 * costoExtra;
}
function RegistrarDocumento(form) {
    form.submit();
}
function EditCostDocument(form) {
    form.action = "/caso/EditCostDocument";
    form.submit();
}
function ExportInvoiceCostDocument(form) {
    form.action = "/pdf/Invoice";
    form.submit();
}
function ExportWSCostDocument(form) {
    form.action = "/pdf/WorkingSheet";
    form.submit();
}

